<?php
function app_die($msg){
	echo $msg;
	die();
}

/**
 * 讀取檔案內容
 * @param string $file
 * @return boolean
 */
function read_file($file){
	if(! file_exists($file))
		return false;
	return file_get_contents($file);
}

/**
 * 將資料寫入檔案
 * @param string $file
 * @param string $data
 * @param string $mode
 * @return boolean
 */
function write_file($file, $data, $mode = 'w'){
	if ( ! $fp = @fopen($file, $mode))
		return false;
	flock($fp, LOCK_EX);
	fwrite($fp, $data);
	flock($fp, LOCK_UN);
	fclose($fp);
	return true;
}

/**
 * 刪除檔案
 * @param string $files
 * @return boolean
 */
function delete_file($files){
	if(is_array($files)){
		foreach($files as $file)
			delete_file($file);
	}elseif(is_string($files))
		unlink($files);
	else
		return false;
	return true;
}

/**
 * 清資料夾
 * @param string $directory
 * @param boolean $empty
 * @source http://www.php.net/manual/en/function.rmdir.php#98499
 */
function clean_dir($directory) {
	if(substr($directory,-1) == "/") {
		$directory = substr($directory,0,-1);
	}

	if(!file_exists($directory) || !is_dir($directory)) {
		return false;
	} elseif(!is_readable($directory)) {
		return false;
	} else {
		$directoryHandle = opendir($directory);
		 
		while ($contents = readdir($directoryHandle)) {
			if($contents != '.' && $contents != '..') {
				$path = $directory . "/" . $contents;
				 
				if(is_dir($path)) {
					clean_dir($path);
				} else {
					@unlink($path);
				}
			}
		}
		 
		closedir($directoryHandle);
		 
		return true;
	}
}

/**
 * 取得檔案資訊
 * @param string $file
 */
function get_file_info($file){
	if ( ! file_exists($file))
	return false;
	return array(
		'name' => substr(strrchr($file, DIRECTORY_SEPARATOR), 1),
		'server_path' => $file,
		'size' => filesize($file),
		'date' => filemtime($file),
		'readable' => is_readable($file),
		'writable' => is_writable($file),
		'executable' => is_executable($file),
		'fileperms' => fileperms($file)
	);
}

/**
* Get either a Gravatar URL or complete image tag for a specified email address.
*
* @param string $email The email address
* @param string $s Size in pixels, defaults to 80px [ 1 - 512 ]
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
* @param boole $img True to return a complete IMG tag False for just the URL
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
* @return String containing either just a URL or a complete image tag
* @source http://gravatar.com/site/implement/images/php/
*/
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
	$url = 'http://www.gravatar.com/avatar/';
	$url .= md5( strtolower( trim( $email ) ) );
	$url .= "?s=$s&d=$d&r=$r";
	if ( $img ) {
		$url = '<img src="' . $url . '"';
		foreach ( $atts as $key => $val )
		$url .= ' ' . $key . '="' . $val . '"';
		$url .= ' />';
	}
	return $url;
}

/**
 * 判斷是不是Email
 * @param string $address
 */
function is_email($address){
	return preg_match("/^(?:[a-z0-9\+_\-]+)(?:\.[a-z0-9\+_\-]+)*@(?:[a-z0-9\-]+\.)+[a-z]{2,6}$/i", $address) ? true: false;
}

/**
 * 隨機取得字串
 * @param int $len
 * @param string $word
 */
function rand_word($len = 32, $word = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_+=<>?{}|\][';/.,-`']"){
	srand(time());
	$randword = '';
	$word_len = strlen($word);
	for ($i = 0; $i < $len; $i++) {
		$randword .= $word[rand() % $word_len];
	}
	return $randword;
}
?>